import { User } from '../../../core/auth/user.service';
import { PasswordService } from '../service/password.service';
import { Component, OnInit } from '@angular/core';
import { Params, ActivatedRoute, Router } from '@angular/router';
import swal from 'sweetalert2';
@Component({
  selector: 'ncloud-change-password',
  templateUrl: './changepassword.html',
  styles: [``]
})
export class ChangePassword implements OnInit {
  public oldPassword = '';
  public newPassword = '';
  public reNewPassword = '';
  public newAccount = '';
  public changeAccount = false;
  public backURL;
  constructor(private route: ActivatedRoute, private router: Router, public user: User, private passwordService: PasswordService) {
    this.newAccount = user.account;
  }
  ngOnInit() {
    this.initParameters();
  }
  private initParameters(): void {
    this.route.params.switchMap((params: Params) => {
      const result: any = [];
      if (params['backURL']) {
        this.backURL = params['backURL'];
      }
      return result;
    }
    ).subscribe((c: any) => { });
  }
  public submit(): void {
    const a = this.newAccount !== '' ? this.newAccount : null;
    this.passwordService.changePassword(this.oldPassword, this.newPassword, a).subscribe((data: any) => {
      if (data.code === '0000') {
        window.sessionStorage.setItem('firstLogin', 'false');
        swal({
          title: '提示!',
          text: '操作已成功!',
          type: 'success',
          confirmButtonColor: '#3085d6',
          confirmButtonText: '确定!',
          confirmButtonClass: 'btn btn-success',
        }).then((e: any) => {
          if (this.backURL) {
            this.router.navigate([this.backURL]);
          } else {
            this.router.navigate(['/']);
          }
        }, (dismiss: any) => {
          if (this.backURL) {
            this.router.navigate([this.backURL]);
          } else {
            this.router.navigate(['/']);
          }
        });
      } else {
        swal({
          title: '提示!',
          text: data.message,
          type: 'error',
          confirmButtonColor: '#3085d6',
          confirmButtonText: '确定!',
          confirmButtonClass: 'btn btn-success',
        }).then((e: any) => {
        }, (dismiss: any) => {
        });
      }
    });
  }
  public cancel(): void {
    swal({
      title: '警告!',
      text: '真的要重置页面吗?',
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: '确定!',
      cancelButtonText: '不，在考虑下!',
      confirmButtonClass: 'btn btn-success',
      cancelButtonClass: 'btn btn-danger'
    }).then((e: any) => {
      this.changeAccount = false;
      this.newPassword = '';
      this.reNewPassword = '';
      this.newAccount = this.user.account;
    }, (dismiss: any) => {
    });
  }
}
